/**
 * 订单管理
 */
var orderManager = {
    id: "orderManagerTable",	//表格id
    table: null,
    layerIndex: -1,
};

/**
 * 初始化表格的列
 */
orderManager.initColumn = function () {
    return [
        // {title: 'ID', field: 'userid', visible: true, align: 'center',valign: 'middle',formatter: orderManager.formatFiled},
        {title: '订单编号', field: 'order_id', visible: true, align: 'center',valign: 'middle'},
        {title: '商品名称', field: 'product_name', visible: true, align: 'center',valign: 'middle',width:'33%',formatter: orderManager.formatProduct_name},
        {title: '订单类型', field: 'type',visible: true, align: 'center',valign: 'middle'},
        {title: '实际付款', field: 'actual_amount', visible: true, align: 'center',valign: 'middle',formatter: orderManager.formatColor},
        {title: '电话号码', field: 'phone',visible: true, align: 'center',valign: 'middle',formatter: orderManager.formatFiled},
        {title: '订单佣金', field: 'rake_back', visible: true, align: 'center',valign: 'middle',formatter:orderManager.formatColor},
        {title: '用户预估返利', field: 'user_rake_back_yugu', visible: true, align: 'center',valign: 'middle'},
        {title: '购买类型', field: 'buytype',visible: true, valign: 'middle', align: 'center',formatter: orderManager.formatBuytype},
        {title: '结算时间', field: 'clear_time',visible: true, valign: 'middle', align: 'center',formatter: orderManager.formatClear_time},
        {title: '创建时间', field: 'create_time',visible: true, align: 'center', valign: 'middle'},
        {title: '订单状态', field: 'status', visible: true, align: 'center',valign: 'middle'},
    ];
};

orderManager.formatProduct_name = function(val, row, index) {
    var name = val;
    if (val.length > 60){
        name = val.substr(0,60) + "...";
    }
    if (name.length > 0 && name.indexOf("<") != -1){
        var rename = name.replace(/</g,"《").replace(/>/g,"》");
        return rename
    }
    return name;
}

orderManager.formatColor = function(val, row, index) {
    return '<span style="color:red">'+val+'</span>';
}

/**
 * 查询表单提交参数对象
 * @returns {{}}
 */
orderManager.formParams = function() {
    var queryData = {};

    queryData['type'] = $("#type").val();
    queryData['startTime'] = $("#startTime").val();
    queryData['endTime'] = $("#endTime").val();
    queryData['status'] = $("#status").val();
    queryData['orderId'] = $("#orderId").val();
    queryData['productName'] = $("#productName").val();
    queryData['nick_name'] = $("#nick_name").val();
    queryData['phone'] = $("#phone").val();
    queryData['app_name'] = $("#app_name").val();

    return queryData;
}

orderManager.formatClear_time = function(val, row) {
    if (row.clear_time.length < 1){
        return "-";
    }
    return row.clear_time;
}

orderManager.formatApp_packageName = function(val, row) {
    if (val.length < 1){
        return "核桃省钱";
    }
    return val;
}

orderManager.formatFiled = function(val) {
    if (val.length < 1){
        return "-";
    }
    return val;
}
orderManager.formatBuytype = function(val, row, index) {
    if (val == 0){
        return "自购";
    }
    if (val == 1){
        return "分享购";
    }
    return "-";
}

/**
 * 查询列表
 */
orderManager.search = function () {
    orderManager.table.refresh({query: orderManager.formParams()});
};


$(function () {
    var defaultColunms = orderManager.initColumn();
    var table = new BSTablePG(orderManager.id, "/orderManager/list", defaultColunms);
    table.setPaginationType("server");
    orderManager.table = table.init();
});
